<?php
/**
 * Created by PhpStorm.
 * User: UEWARE
 * Date: 2019-9-3
 * Time: 18:12:43
 */

namespace App\Http\Controllers\WeChat\Diary\V1\Travels;

use App\Http\Controllers\WeChat\WeChatController;
use App\Models\TravelsContentModel;
use App\Models\TravelsModel;
use App\Models\UserModel;
use Illuminate\Http\Request;

class DetailController extends WeChatController
{
    /**
     * 游记详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        // 获取当前data值
        $data = $request->input();

        $travels = TravelsModel::query()->where('id', $data['id'])->first();

        if (empty($travels)) {
            return success();
        }

        $travels = $travels->toArray();
        // 用户
        $user = UserModel::query()->where('id', $travels['user_id'])->first()->toArray();

        $travels['headimgurl'] = $user['headimgurl'];
        $travels['nickname'] = $user['nickname'];
        $travels['created_at'] = date('Y.m.d H:i', $travels['created_at']);

        $travels_content = TravelsContentModel::query()->where('t_id', $data['id'])->get()->toArray();

        $content = [];
        $contents = [];
        foreach ($travels_content as $key => $val) {
            $val['address'] = json_decode($val['address'], true)['name'];
            $val['created_at'] = date('Y.m.d H:i', $val['created_at']);
            $content[$val['address']][] = $val;
        }

        $i = 0;
        foreach ($content as $key => $val) {
            $contents[$i]['address'] = $key;
            $contents[$i]['list'] = $val;
            $i++;
        }

        return success(['detail' => $travels, 'content' => $contents]);
    }
}